﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using t_TestingLibrary;
using System.ComponentModel.Composition;
using PaperRenderingInterfaces;
using DeepZoomPivotConstructor.PivotLib;
using HTMLPageScanners;
using System.IO;
using System.Diagnostics;

namespace t_HTMLPageScanners
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class t_MasterHTMLOnlyBelow
    {
        [TestInitialize]
        public void Setup()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void Cleanup()
        {
            Utilities.MyClassDone();
        }

        /// <summary>
        /// Load a file instead of accessing the web.
        /// </summary>
        [Export(typeof(IWebAccess))]
        private class LoadAFile : IWebAccess
        {
            public FileInfo theFileToLoad;
            public string UrlAskedFor { get; set; }

            public string GetUriAsString(Uri whatToLoad)
            {
                UrlAskedFor = whatToLoad.OriginalString;

                return LoadFile(theFileToLoad);
            }

            /// <summary>
            /// Load up the req' file.
            /// </summary>
            /// <param name="finfo"></param>
            /// <returns></returns>
            private string LoadFile(FileInfo finfo)
            {
                if (finfo.Exists)
                {
                    using (var reader = finfo.OpenText())
                    {
                        return reader.ReadToEnd();
                    }
                }
                else
                {
                    return null;
                }
            }


            public DateTime? GetLastModificationTime(Uri whatToCheck)
            {
                return DateTime.Now;
            }
        }

        /// <summary>
        /// Helper class that doesn't actually do any rendering as much as hold onto things.
        /// </summary>
        [Export(typeof(IPaperRenderEngine))]
        class DummyRender : IPaperRenderEngine
        {
            public List<string> addressesRequested = new List<string>();

            public ScanThisPageAndBelow sp = null;

            public IEnumerable<PivotImage> Render(string renderName, string renderAddress)
            {
                if (renderName == "MasterHTMLRenderHTMLAndFurther")
                {
                    foreach (var item in sp.Render(renderAddress))
                    {
                        yield return item;
                    }
                }
                else
                {
                    Trace.WriteLine("Rendering " + renderName + " - " + renderAddress);
                    if (!addressesRequested.Contains(renderAddress))
                    {
                        addressesRequested.Add(renderAddress);
                        PivotImage pm = new PivotImage(null);
                        pm.Name = "HSF1";
                        yield return pm;
                    }
                }
            }
        }

        [TestMethod]
        public void TestCTor()
        {
            var h = new MasterHTMLOnlyBelow();
        }
        
        [TestMethod]
        public void TestTevatronCombinedLimitsPage()
        {
            var target = new MasterHTMLOnlyBelow();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_HTMLPageScanners\\combined.html") };
            Utilities.AddPart(laf);
            var rd = new DummyRender();
            rd.sp = new ScanThisPageAndBelow();
            Utilities.AddPart(rd);
            Utilities.AddPart(rd.sp);
            Utilities.Compose(target);

            var result = target.Render("http://tevnphwg.fnal.gov/");
            var results = result.ToArray();
            Assert.AreEqual(11, results.Count(), "Incorrect number of sub-web pages parsed!");

            var allTitles = from i in results
                            from f in i
                            where f.Name == "Note Title"
                            let fac = (PivotMetadataString)f
                            select fac.Value;

            Assert.AreEqual(11, allTitles.Count(), "Missing some titles!");

        }
    }
}
